import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from "react";
import DropDownButton from "devextreme-react/drop-down-button";
import CpasIcon from "~/public/cpas-ui/components-ui/components/Icon";
import { Modal } from "antd";
import CpasModalComponent from "../Validator";
import { defaultHeaderStyle } from "../../HandSonGrid";
import { consts } from "../../../../tools/consts";
import axios from "axios";
import notify from "devextreme/ui/notify";
import { getZcbmId, getCurrentSysName, SystemName, MergeType, getHbTypeByZcbfid, } from "../../../../tools/Tools";
import { ROOT_URL } from "../../../../tools/HttpClient";
const { confirm } = Modal;
const apiUrl = ROOT_URL;
const dbname = window.sessionStorage.electronParams
    ? JSON.parse(window.sessionStorage.electronParams)?.params?.DBName
    : consts.defaultDbName; // "202205188806-KH202012975";;
// "1-AUD-2023-002";
/**
 * 保存设计表
 * @param serverData
 */
export const singleDSTableInit = async (templateName, tableNames, type) => {
    try {
        const response = await axios.post(`${apiUrl}/template/singleDSTableInit`, {
            dbname,
            type,
            templateName,
            tableNames,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
// const needDeleteCustomConfig = ["bl", "r", "ht", "bg", "fc", "it", "fs"];
const GetDataAndValidator = ({ _this }) => {
    // 勾稽校验设置弹框
    const [isOpen, setIsOpenModal] = useState(false);
    const onShowModal = () => {
        setIsOpenModal(!isOpen);
    };
    /**
     * 重置所有表样式，需求说暂时不要。先注释掉。 该功能通过测试
     */
    const handleResetTableStyle = () => {
        _this.gridRef.gridService.resetAllCellsMetaStyleConfig();
        resetColumnConfig();
        resetRowConfig();
        _this.gridRef.reRender();
        // message.info("重置样式表成功");
    };
    const deleteObjProp = (columnMeta, needDeleteCustomConfig) => {
        for (let i = 0, length = needDeleteCustomConfig.length; i < length; i++) {
            const propName = needDeleteCustomConfig[i];
            delete columnMeta[propName];
        }
    };
    const resetTableHeaderStyle = (config) => {
        for (let propName in defaultHeaderStyle) {
            const propValue = defaultHeaderStyle[propName];
            config[propName] = propValue; // config.ht = 2;
        }
    };
    const resetColumnConfig = () => {
        const headerColumn = _this.headerService.headerColumn;
        for (let col = 0, length = headerColumn.length; col < length; col++) {
            const colData = headerColumn[col];
            const config = colData?.config;
            delete config.custom;
            delete config.style;
            resetTableHeaderStyle(config);
            let update_config_headerJson = _this.headerService.updateHeaderConfigByIndex(_this.headerService.headJson, col, {});
            _this.headerService.headerCompute(update_config_headerJson);
        }
        //刷新公式@todo迁移后台
        _this.formulaService.refreshAllPolymeric();
        //_this.formulaService.refreshAllPolymericRow();
        //_this.formulaService.refreshAllPolymericColumn();
    };
    const resetRowConfig = () => {
        console.log("重置行配置");
        const gridData = _this.gridRef.data;
        const lastHeader = _this.headerService.lastHeader;
        for (let row = 0; row < gridData.length; row++) {
            const config = JSON.parse(gridData[row][lastHeader]);
            delete config.custom;
            delete config.style;
            gridData[row][lastHeader] = JSON.stringify(config);
        }
        //数据同步
        //this.context.data = gridData;
        _this.hot.updateData(gridData);
        //刷新公式@todo迁移后台
        _this.formulaService.refreshAllPolymeric();
        //_this.formulaService.refreshAllPolymericRow();
        //_this.formulaService.refreshAllPolymericColumn();
    };
    const getTemplateInfo = async (zcbfid) => {
        try {
            const { data } = await axios.post(`${apiUrl}/sqlapis/getApiData?api=account.seekAuditReportTemplate`, {
                name: "account.seekAuditReportTemplate",
                params: {
                    zcbfid: zcbfid,
                },
            });
            return data;
        }
        catch (e) {
            _this?.showErrorNotify("查询首页模板信息失败");
        }
    };
    const handleClickInit = async () => {
        //TODO 获取当前表格的来源，通过吕哥的树表字段决定. dataSource分为模板导入还是新建节点
        //如果是模板导入恢复到sqlite中的模板状态，如果是新建节点，恢复到默认状态
        // const dataSource = "create"; //先默认是新建节点,等模板导出在来做这个逻辑*/}
        //  if (dataSource === "create") { // 变四格
        //   _this.gridRef.resetTable("ds");
        //  }
        const zcbfid = getZcbmId();
        const result = await getTemplateInfo(zcbfid);
        const currentTableName = _this.gridRef.props.name.props?.children[0] || _this.gridRef.props.name;
        console.log("_this:", _this);
        console.log("_this.gridRef:", _this.gridRef);
        console.log("_this.gridRef.props:", _this.gridRef.props);
        const type = _this.gridRef.props.type;
        let tableNames = [];
        tableNames.push(currentTableName);
        if (result.ok) {
            const templateName = result.data?.AuditReportTemplate;
            const response = await singleDSTableInit(templateName, tableNames, type);
            const iGridData = response.data;
            const tableConfig = JSON.parse(iGridData.tableConfig);
            console.log("tableConfig前", iGridData.tableConfig);
            tableConfig.formulaOff = _this.gridRef.tableConfig.formulaOff;
            tableConfig.clearTable = _this.gridRef.tableConfig.clearTable;
            iGridData.tableConfig = JSON.stringify(tableConfig);
            console.log("tableConfig后:", iGridData.tableConfig);
            if (iGridData) {
                handleResetTableStyle();
                _this.gridRef.reloadTable(iGridData);
                notify("重置数据成功", "success", 2000);
            }
            else {
                confirm({
                    title: "提示",
                    icon: "",
                    content: "模板中未找到该披露表 , 是否要进行表格重置？",
                    onOk() {
                        handleResetTableStyle();
                        _this.gridRef.resetTable(type);
                    },
                    onCancel() { },
                });
            }
        }
        _this.gridRef.setTableContentChange(true, {
            type: "单个披露表初始化",
            params: null,
        });
    };
    const sysName = getCurrentSysName();
    const mergeType = getHbTypeByZcbfid();
    return (_jsxs("div", { className: "table-design-tools data-design data-validator", children: [_jsxs("div", { className: "col col1", children: [(sysName === SystemName.SINGLE ||
                        mergeType === MergeType.TEMPLATE) && (_jsx("div", { className: "row row1", children: _jsx("span", { children: "\u53D6\u6570\u4E0E\u6821\u9A8C" }) })), sysName === SystemName.SINGLE && (_jsxs("div", { className: "row row2", children: [_this.gridRef.props.type === "ds" ? (_jsx("div", { className: "tools-item-width-auto", children: _jsx(DropDownButton, { width: 120, 
                                    // disabled // 仅用于608打包
                                    disabled: _this.gridRef.props.type === "ds" && !_this.state.formulaOff
                                        ? false
                                        : true, text: "\u53D6\u6570\u8BBE\u7F6E", icon: "iconfont icon-shujuyuan", className: "min-width-dx-button", dropDownOptions: _this.buttonDropDownOptions, displayExpr: "name", items: _this.insertRows, onItemClick: _this.onItemClick }) })) : null, _this.gridRef.props.type === "ds" && (_jsxs("div", { className: "tools-item-width-auto", onClick: () => {
                                    _this.gridRef.resetLogicFormulaState();
                                }, children: [_jsx("div", { className: "tools-item", children: _jsx(CpasIcon, { type: "icon-zhongzhidongtaihangliezhuangtai", className: "cpas-icon" }) }), _jsx("span", { children: "\u91CD\u7F6E\u52A8\u6001\u884C\u5217" })] }))] })), _jsxs("div", { className: "row row3", children: [(sysName === SystemName.SINGLE ||
                                mergeType === MergeType.TEMPLATE) && (_jsxs("div", { className: "tools-item-width-auto", style: {
                                    width: 120,
                                }, onClick: () => {
                                    _this.gridRef.clearTableData();
                                }, children: [_jsx("div", { className: "tools-item", children: _jsx(CpasIcon, { type: "icon-qingkongshuju", className: "cpas-icon" }) }), _jsx("span", { children: "\u6E05\u7A7A\u6570\u636E" })] })), _this.gridRef.props.type !== "ds" && (_jsx("div", { className: "tools-item-width-auto", children: _jsx(DropDownButton, { width: 115, 
                                    //disabled={_this.gridRef.props.type === "ds" ? true : false} //暂时注掉，仅用于608打包
                                    text: "\u6821\u9A8C\u516C\u5F0F", icon: "iconfont icon-xiaoyan", className: "min-width-dx-button", onButtonClick: () => {
                                        onShowModal();
                                        _this.gridRef.setTableContentChange(true, {
                                            type: "校验公式",
                                            params: null,
                                        });
                                    } }) })), (sysName === SystemName.SINGLE ||
                                mergeType === MergeType.TEMPLATE) && (_jsxs("div", { className: "tools-item-width-auto", onClick: () => {
                                    _this.gridRef.resetTable("ds");
                                }, style: { minWidth: 100 }, children: [_jsx("div", { className: "tools-item", children: _jsx(CpasIcon, { type: "icon-clear", className: "cpas-icon" }) }), _jsx("span", { children: "\u91CD\u7F6E\u8868\u6837" })] }))] })] }), _jsxs("div", { className: "col col2", children: [_jsx("div", { className: "row row1", children: _jsx("span", {}) }), _jsxs("div", { className: "row row2 row-border-left", children: [_jsxs("div", { className: "tools-item-width-auto", onClick: () => {
                                    _this.syncRowMas();
                                }, children: [_jsx("div", { className: "tools-item", children: _jsx(CpasIcon, { type: "icon-zidongtongbuhangguanxi1", className: "cpas-icon" }) }), _jsx("span", { children: "\u540C\u6B65\u884C\u5173\u7CFB" })] }), (sysName !== SystemName.MERGE ||
                                mergeType === MergeType.TEMPLATE) && (_jsxs("div", { className: "tools-item-width-auto", onClick: () => {
                                    _this.syncColumnMaps();
                                }, children: [_jsx("div", { className: "tools-item", children: _jsx(CpasIcon, { type: "icon-zidongtongbuhangguanxi1", className: "cpas-icon" }) }), _jsx("span", { children: "\u540C\u6B65\u5217\u5173\u7CFB" })] }))] }), _jsx("div", { className: "row row3 row-border-left", children: _jsxs("div", { className: "tools-item-width-auto", onClick: () => {
                                _this.setRowColumnsMap();
                            }, children: [_jsx("div", { className: "tools-item", children: _jsx(CpasIcon, { type: "icon-pipeihanglieguanxi", className: "cpas-icon" }) }), _jsx("span", { children: "\u663E\u793A\u884C\u5217\u5173\u7CFB" })] }) })] }), _jsx(CpasModalComponent, { isOpen: isOpen, handleOpen: onShowModal, params: _this })] }));
};
//_this.gridRef.articulationVerifyService
export default GetDataAndValidator;
